package jxtend.processor.eclipse.visitor.interceptor;

import jxtend.processor.eclipse.visitor.handler.impl.TypeUtils;

import org.eclipse.jdt.internal.compiler.ast.ASTNode;
import org.eclipse.jdt.internal.compiler.ast.MethodDeclaration;
import org.eclipse.jdt.internal.compiler.lookup.ExtraCompilerModifiers;
import org.eclipse.jdt.internal.compiler.lookup.Scope;
import org.eclipse.jdt.internal.compiler.lookup.TagBits;

@SuppressWarnings("restriction")
public class FixNullReturnTypeOfMethodBindingInterceptor extends ASTNodeVisitorInterceptorAdapter {

//	private static final ResultMethod3Args<LookupEnvironment, ReferenceBinding, char[][], Boolean, Boolean>
//		getTypeFromCompoundNameMethodOfLookupEnvironment = ReflectMethod.method(LookupEnvironment.class,
//			ReferenceBinding.class, "getTypeFromCompoundName", char[][].class, boolean.class, boolean.class);

	@Override
	public boolean interceptVisit(ASTNodeVisitorInterceptorContext interceptorChain, ASTNode node, Scope scope) {
		if (node instanceof MethodDeclaration && ((MethodDeclaration) node).binding != null
				&& ((MethodDeclaration) node).binding.returnType == null) {
			MethodDeclaration methodDeclaration = (MethodDeclaration) node;

			methodDeclaration.binding.returnType = methodDeclaration.returnType.resolveType(methodDeclaration.scope);
//			methodDeclaration.binding.returnType = TypeUtils.createUnresolvedTypeBinding(
//					scope, methodDeclaration.returnType, methodDeclaration.typeParameters());

//			LookupEnvironment lookupEnvironment = scope.referenceCompilationUnit().scope.environment;
//
//			TypeReference returnType = ((MethodDeclaration) node).returnType;
//			methodDeclaration.binding.returnType = getTypeFromCompoundNameMethodOfLookupEnvironment.invoke(
//					lookupEnvironment, returnType.getTypeName(),
//					(returnType instanceof ParameterizedSingleTypeReference)
//						|| (returnType instanceof ParameterizedQualifiedTypeReference),
//					false);

			methodDeclaration.binding.modifiers &= ~ExtraCompilerModifiers.AccUnresolved;

//			System.out.println("### NEW methodDeclaration.binding.returnType: " + methodDeclaration.binding.returnType);
		}

		return super.interceptVisit(interceptorChain, node, scope);
	}
}
